package com.ppp.poi.service;

import com.ppp.poi.utils.RestClient;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 发送Http工具类
 *
 * @author Wu Jin
 * @create 2018-12-28
 */
@Slf4j
@Service
public class HttpService {

    private RestTemplate restTemplate = RestClient.getClient();

    public <T> ResponseEntity<T> doPost(String url, MultiValueMap<String, String> paramMap, HttpHeaders headers, Class<T> clazz) {
        log.info("post url : {} \nparamMap : {} \n headers : {}", url, paramMap, headers);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<T> stringResponseEntity = restTemplate.postForEntity(url, httpEntity, clazz);
        return stringResponseEntity;
    }

    public <T> ResponseEntity<T> doPost(String url, MultiValueMap<String, String> paramMap, Class<T> clazz) {
        HttpHeaders headers = new HttpHeaders();
        return doPost(url, paramMap, headers, clazz);
    }


    public <T> T doPostForPojo(String url, MultiValueMap paramMap, HttpHeaders headers, Class<T> clazz) {
        log.info("post url : {} \nparamMap : {} \n headers : {}", url, paramMap, headers);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<T> stringResponseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, clazz);
        return stringResponseEntity.getBody();
    }

    public <T> T doPostForPojo(String url, MultiValueMap<String, String> paramMap, Class<T> clazz) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.valueOf("*/*")));
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        return doPostForPojo(url, paramMap, headers, clazz);
    }

    public <T> T doPostForPojo(String url, Class<T> clazz) {
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
        return doPostForPojo(url, paramMap, clazz);
    }

    public <T> ResponseEntity<T> doGet(String url, Class<T> clazz) {
        log.info("get url : {} ", url);
        ResponseEntity<T> stringResponseEntity = restTemplate.getForEntity(url, clazz);
        return stringResponseEntity;
    }

    public <T> ResponseEntity<T> doGet(String url, MultiValueMap<String, String> paramMap, Class<T> clazz) {
        log.info("get url : {} \nparamMap : {}", url, paramMap);
        return restTemplate.getForEntity(url, clazz, paramMap);
    }


    public <T> T doGetForPojo(String url, MultiValueMap<String, String> paramMap, Class<T> clazz) {
        log.info("get url : {} \nparamMap : {} ", url, paramMap);
        return restTemplate.getForObject(url, clazz, paramMap);
    }

    public <T> T doGetForPojo(String url, Class<T> clazz) {
        log.info("get url : {} ", url);
        return restTemplate.getForObject(url, clazz);
    }


}
